package com.fzg.common.demo.country;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * create by fzg
 * 2022/11/21 10:41
 */

@Slf4j
public class ProvinceCityAndCounty {

    public static void main(String[] args) {
        // txt文本文件的位置
        String txtUrl = "E:\\学习\\项目\\vue-vant-app\\myBlog\\myCaveolae_server\\src\\main\\java\\com\\fzg\\common\\demo\\country\\province.txt";
        ArrayList<HashMap<String, Object>> chineseAllProvinces = getChineseAllProvincesAndCityCounty(txtUrl);
        log.info(JSON.toJSONString(chineseAllProvinces));
    }

    /**
     * 获取中国所有省份中文名和英文名
     * @param txtUrl 文件地址
     * @return ArrayList<HashMap<String,Object>>
     */
    public static ArrayList<HashMap<String,Object>> getChineseAllProvincesAndCityCounty(String txtUrl){
        // 根据文件url读取文件内容
        File file = new File(txtUrl);
        BufferedReader bf;
        String data = "";
        try {
            bf = new BufferedReader(new FileReader(file));
            data = bf.readLine();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            log.info("文件未找到");
        } catch (IOException e) {
            e.printStackTrace();
            log.info("文件读取失败");
        }
        if (data.equals("")){
            log.info("文件内容为空");
            return null;
        }else {
            log.info("文件读取成功");
            log.info(data);
            return convertContent(data);
        }
    }

    /**
     * 处理文件内容
     * @param data 参数
     * @return ArrayList<HashMap<String,Object>>
     */
    public static ArrayList<HashMap<String,Object>> convertContent(String data){
        // 文件内容，把大括号前面后面都剔除 var name = { }; 我们要的是大括号中间的部分
        String res = data.substring(data.indexOf("{"), data.lastIndexOf("}") + 1);
        // 将文件内容里的单引号全部替换成双引号
        res = res.replace("'","\"");
        log.info(res);
        // 现在res 已经是一个标准的JSON字符串了，把他解析成对象就行了
        JSONObject jsonObject = new JSONObject(JSON.parseObject(res));
        // log.info(jsonObject.toString());
        // 将JSONObject转为list
        Map<String, Object> innerMap = jsonObject.getInnerMap();
        Set<Map.Entry<String, Object>> entries = innerMap.entrySet();
        ArrayList<HashMap<String, Object>> list = new ArrayList<>();
        for (Map.Entry<String, Object> entry : entries) {
            HashMap<String, Object> map = new HashMap<>();
            JSONArray objects = JSON.parseArray(entry.getValue().toString());
            map.put("code",entry.getKey());
            map.put("parentCode",objects.get(1));
            map.put("chineseName",objects.get(0));
            map.put("englishName",objects.get(2));
            list.add(map);
        }
        log.info(list.toString());
        if (list.size() > 0){
            log.info("解析成功");
        }else {
            log.info("解析失败");
        }
        return list;
    }

}
